package com.team.common.dynamic.impl;

import com.team.common.dynamic.ModifyBeanDefinition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.filter.TypeFilter;

import java.util.Set;
@Slf4j
public class CustomerPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {

    private ModifyBeanDefinition modifyBeanDefinition;

    public CustomerPathBeanDefinitionScanner(BeanDefinitionRegistry registry,
                                             boolean useDefaultFilters,
                                             Environment environment,
                                             ResourceLoader resourceLoader, ModifyBeanDefinition modifyBeanDefinition) {
        super(registry, useDefaultFilters, environment, resourceLoader);
        this.modifyBeanDefinition = modifyBeanDefinition;
    }

    @Override
    protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
        super.registerBeanDefinition(definitionHolder, registry);
    }

    @Override
    protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
        log.info("xxxxxxxxxxxx");
        Set<BeanDefinitionHolder> holders = super.doScan(basePackages);
        if(CollectionUtils.isNotEmpty(holders)){
           modifyBeanDefinition.modify(holders);
        }
        return holders;
    }

    @Override
    protected void registerDefaultFilters() {
        addIncludeFilter(new CustomerTypeFilter(CustomerTypeFilter.class));
    }

    @Override
    public void addIncludeFilter(TypeFilter includeFilter) {
        super.addIncludeFilter(includeFilter);
    }

    @Override
    public void addExcludeFilter(TypeFilter excludeFilter) {
        super.addExcludeFilter(excludeFilter);
    }
}
